package com.jm.offer.easy;

import com.jm.offer.TreeNode;
import sun.reflect.generics.tree.Tree;

/**
 * @Description https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
 * @date 2022/4/26 8:07
 */
public class _54二叉搜索树的第k大节点 {
    public static void main(String[] args) {
        // [5,3,6,2,4,null,null,1]
        TreeNode root = new TreeNode(5);
    }
    int res, k;
    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return res;
    }
    void dfs(TreeNode root) {
        if(root == null) {
            return;
        }
        dfs(root.right);
        if(k == 0) {
            return;
        }
        if(--k == 0) {
            res = root.val;
        }
        dfs(root.left);
    }
}
